package com.mmf.ancientcostume.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.mmf.ancientcostume.R;
import com.mmf.ancientcostume.adapter.goods.CommentListAdapter;
import com.mmf.ancientcostume.base.activity.BaseTitleActivity;
import com.mmf.ancientcostume.base.adapter.BaseClickRecyclerAdapter;
import com.mmf.ancientcostume.model.Comment;
import com.mmf.ancientcostume.model.StaticData;
import com.mmf.ancientcostume.presenter.imp.goods.CommentListPresenterImp;
import com.mmf.ancientcostume.view.BaseView;
import com.mmf.ancientcostume.widget.LoadRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by MMF on 2017-10-17.
 */

public class CommentListActivity extends BaseTitleActivity<CommentListPresenterImp, CommentListActivity> implements BaseView<List<Comment>>, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.rv_goods_list)
    LoadRecyclerView rvCommentList;
    @BindView(R.id.srl_refresh)
    SwipeRefreshLayout srlRefresh;
    private CommentListAdapter adapter;
    private int type;   //GOODS_DETAIL_TO_COMMENT：表示商品评论列表 USER_TO_COMMENT：表示个人评论列表
    private int id;
    private int pageNo = 1;

    @Override
    public int getLayout() {
        return R.layout.activity_goods_list;
    }

    @Override
    public void init() {
        type = getIntent().getIntExtra("type", 1); //GOODS_DETAIL_TO_COMMENT：表示商品评论列表 USER_TO_COMMENT：表示个人评论列表
        id = getIntent().getIntExtra("id", 0);
        initAdapter();
    }

    /**
     * 初始化列表适配器
     */
    private void initAdapter() {
        rvCommentList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new CommentListAdapter(this);
        rvCommentList.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseClickRecyclerAdapter.OnItemClickListener<Comment>() {
            @Override
            public void onItemClick(View view, int position, Comment item) {

            }
        });
        srlRefresh.setOnRefreshListener(this);
        //判断是否滑动到最后一条记录，是就加载数据
        rvCommentList.setLoadingDataListener(new LoadRecyclerView.LoadingDataListener() {
            @Override
            public void onLoadMore() {
                pageNo++;
                getData();
            }
        });

    }

    @Override
    protected CommentListPresenterImp getPresenter() {
        return new CommentListPresenterImp();
    }

    @Override
    protected void getData() {
        presenter.getCommentList(getParams(), pageNo);
    }

    private Map<String, Object> getParams() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageNo", pageNo);
        map.put("pageSize", 10);
        if (type == StaticData.GOODS_DETAIL_TO_COMMENT) {
            map.put("goodsId", id);
        } else {
            map.put("userId", id);
        }
        return map;
    }

    @Override
    public void onSuccess(List<Comment> object) {
        srlRefresh.setRefreshing(false);
        adapter.setItems(object);
    }

    @Override
    public void onError(String object) {
        srlRefresh.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        srlRefresh.setRefreshing(true);
        getData();
    }
}
